package net.gz01.wallet.web.config;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import net.gz01.util.ZOThreadPoolExecutor;
import net.gz01.wallet.web.auth.Authority;
import net.gz01.wallet.web.config.route.AdminRoute;
import net.gz01.wallet.web.config.route.ApiRoute;
import net.gz01.wallet.web.config.route.CustomerRoute;
import net.gz01.wallet.web.config.route.MerchRoute;
import net.gz01.wallet.web.config.route.OrgRoute;
import net.gz01.wallet.web.controller.IndexController;
import net.gz01.wallet.web.interceptors.AuthInterceptor;
import net.gz01.wallet.web.interceptors.ExceptionInterceptor;
import net.gz01.wallet.web.interceptors.SessionTimeOutInterceptor;
import net.gz01.wallet.web.model.CardBin;
import net.gz01.wallet.web.noitfy.NotifyThread;

public class SystemConfig extends JFinalConfig{

	public static final Prop config = PropKit.use("config.properties");
	public static final Prop channelConfig = PropKit.use("channelConfig.properties");

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(config.getBoolean("dev"));
		me.setViewType(ViewType.JFINAL_TEMPLATE);
		me.setJsonFactory(new FastJsonFactory());
		me.setError404View("/WEB-INF/error/404.html");
	}

	@Override
	public void configEngine(Engine me) {
		me.setDevMode(config.getBoolean("dev"));
		me.addDirective("auth", Authority.class);
		me.addSharedFunction("/WEB-INF/customer/common/_customer_paginate.html");
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		int corePoolSie = SystemConfig.config.getInt("thread_corePoolSize");
		int maximumPoolSize = SystemConfig.config.getInt("thread_maximumPoolSize");
		long keepAliveTime = SystemConfig.config.getLong("thread_keepAliveTime");
		ZOThreadPoolExecutor.init(corePoolSie, maximumPoolSize, keepAliveTime, TimeUnit.HOURS, new LinkedBlockingQueue<>());
		System.out.println("------------------- 初始化线程池配置成功 -------------------");
		CardBin.reload();
		NotifyThread.getInstance().start();
	}

	@Override
	public void configHandler(Handlers arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		me.add(new SessionTimeOutInterceptor());
		me.add(new AuthInterceptor());
		me.add(new ExceptionInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		final Prop db = PropKit.use("db.properties");
		boolean showSql = db.getBoolean("showSql");
		String url = db.get("jdbcUrl");
		String userName = db.get("userName");
		String password = db.get("password");
		int initialSize = db.getInt("initialSize");
		int minIdle = db.getInt("minIdle");
		int maxActive = db.getInt("maxActive");
		//后台数据源
		DruidPlugin mainDB = new DruidPlugin(url,userName,password);
		mainDB.set(initialSize, minIdle, maxActive);
		me.add(mainDB);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(mainDB);
		arp.setShowSql(showSql);
		MappingKit.mapping(arp);
		me.add(arp);

		//redis缓存
		RedisPlugin redis = new RedisPlugin("main", SystemConfig.config.get("redis_host"),  Integer.parseInt(SystemConfig.config.get("redis_port")),
				SystemConfig.config.get("redis_password"));
		me.add(redis);
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new CustomerRoute());
		me.add(new AdminRoute());
		me.add(new OrgRoute());
		me.add(new MerchRoute());
		me.add(new ApiRoute());
		// 首页
		me.setBaseViewPath("/");
		me.add("/", IndexController.class,"/");
	}

}
